package com.fr.web.platform.entry;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.dav.Env;
import com.fr.base.dav.FileNode;
import com.fr.report.TemplateWorkBook;
import com.fr.report.parameter.Parameter;
import com.fr.schedule.ScheduleContext;
import com.fr.schedule.dao.JdbcDaoManager;
import com.fr.schedule.entry.IdGenerator;
import com.fr.schedule.output.ScheduledOutput;
import com.fr.schedule.task.ScheduleTask;
import com.fr.schedule.trigger.CalendarITrigger;
import com.fr.schedule.trigger.ITrigger;
import com.fr.schedule.trigger.OnceITrigger;
import com.fr.schedule.trigger.SimpleITrigger;
import com.fr.third.org.quartz.JobDataMap;
import com.fr.third.org.quartz.JobDetail;
import com.fr.third.org.quartz.Scheduler;
import com.fr.third.org.quartz.SchedulerException;
import com.fr.third.org.quartz.Trigger;
import com.fr.web.platform.TransmitParameters;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EntryManager
{
  public static FolderEntry getRootFolderEntry()
  {
    FolderEntry localFolderEntry = new FolderEntry("管理目录", "This is the root.");
    localFolderEntry.setId(-1L);
    localFolderEntry.setParentId(-2L);
    return localFolderEntry;
  }

  public static JSONArray getFolderEntrys()
  {
    JSONArray localJSONArray = new JSONArray();
    try
    {
      JSONObject localJSONObject = getRootFolderEntry().createJSONConfig();
      localJSONArray.put(localJSONObject);
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return localJSONArray;
  }

  public static JSONArray getContentEntrys(String paramString)
    throws JSONException
  {
    long l = Long.parseLong(paramString);
    if (l < 0L)
      return null;
    FolderEntry localFolderEntry = (FolderEntry)ScheduleContext.getJdbcDaoManager().load(FolderEntry.class, l);
    JSONArray localJSONArray = localFolderEntry.createContentJSON();
    return localJSONArray;
  }

  public static JSONObject addFolder(String paramString)
  {
    JSONObject localJSONObject;
    try
    {
      localJSONObject = new JSONObject(paramString);
      FolderEntry localFolderEntry = new FolderEntry();
      localFolderEntry.parseJSON(localJSONObject);
      ScheduleContext.getJdbcDaoManager().saveOrUpdate(localFolderEntry);
      return localFolderEntry.createJSONConfig();
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return null;
  }

  public static JSONObject addURLEntry(String paramString)
    throws Exception
  {
    JSONObject localJSONObject = new JSONObject(paramString);
    URLEntry localURLEntry = new URLEntry();
    localURLEntry.parseJSON(localJSONObject);
    ScheduleContext.getJdbcDaoManager().saveOrUpdate(localURLEntry);
    return localURLEntry.createJSONConfig();
  }

  public static void deleleFolderByParentId(String paramString)
  {
    int j;
    int k;
    int i1;
    long l = Long.parseLong(paramString);
    FolderEntry localFolderEntry = (FolderEntry)ScheduleContext.getJdbcDaoManager().load(FolderEntry.class, l);
    List localList1 = localFolderEntry.listFolderEntrys();
    if (localList1 != null)
    {
      int i = 0;
      j = localList1.size();
      while (i < j)
      {
        deleleFolderByParentId(String.valueOf(((FolderEntry)localList1.get(i)).getId()));
        ++i;
      }
    }
    List localList2 = localFolderEntry.listReportletEntrys();
    if (localList2 != null)
    {
      j = 0;
      k = localList2.size();
      while (j < k)
      {
        delReports("2" + ((ReportletEntry)localList2.get(j)).getId());
        ++j;
      }
    }
    List localList3 = localFolderEntry.listFileEntrys();
    if (localList3 != null)
    {
      k = 0;
      i1 = localList3.size();
      while (k < i1)
      {
        delReports("1" + ((FileEntry)localList3.get(k)).getId());
        ++k;
      }
    }
    List localList4 = localFolderEntry.listURLEntrys();
    if (localList4 != null)
    {
      i1 = 0;
      int i2 = localList3.size();
      while (i1 < i2)
      {
        delReports("3" + ((FileEntry)localList3.get(i1)).getId());
        ++i1;
      }
    }
    ScheduleContext.getJdbcDaoManager().deleteById(FolderEntry.class, l);
  }

  public static JSONArray getReportletsOfCurrentEnv(String paramString, IdGenerator paramIdGenerator)
  {
    JSONArray localJSONArray = new JSONArray();
    FileNode[] arrayOfFileNode = null;
    try
    {
      arrayOfFileNode = FRContext.getCurrentEnv().listFile(paramString);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    if ((arrayOfFileNode == null) || (arrayOfFileNode.length == 0))
      return localJSONArray;
    for (int i = 0; i < arrayOfFileNode.length; ++i)
    {
      FileNode localFileNode = arrayOfFileNode[i];
      String str = localFileNode.getEnvPath().substring("reportlets".length() + 1);
      JSONObject localJSONObject = new JSONObject();
      localJSONArray.put(localJSONObject);
      try
      {
        localJSONObject.put("id", paramIdGenerator.generateId());
        localJSONObject.put("text", localFileNode.getName());
        localJSONObject.put("path", str);
        localJSONObject.put("complete", true);
        localJSONObject.put("isexpand", false);
        if (localFileNode.isDirectory())
        {
          localJSONObject.put("hasChildren", true);
          localJSONObject.put("ChildNodes", getReportletsOfCurrentEnv(localFileNode.getEnvPath(), paramIdGenerator));
        }
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
    }
    return localJSONArray;
  }

  public static JSONObject addReport(String paramString)
  {
    JSONObject localJSONObject;
    try
    {
      localJSONObject = new JSONObject(paramString);
      String str = localJSONObject.getString("path");
      int i = str.lastIndexOf(".");
      if ((i >= 0) && (ComparatorUtils.equals(str.substring(i), ".cpt")))
      {
        ReportletEntry localReportletEntry = new ReportletEntry();
        localReportletEntry.parseJSON(localJSONObject);
        ScheduleContext.getJdbcDaoManager().saveOrUpdate(localReportletEntry);
        return localReportletEntry.createJSONConfig();
      }
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return null;
  }

  public static void delReports(String paramString)
  {
    String[] arrayOfString = paramString.split(",");
    for (int i = 0; i < arrayOfString.length; ++i)
    {
      String str = arrayOfString[i].substring(0, 1);
      int j = Integer.parseInt(arrayOfString[i].substring(1));
      if (str.equals("2"))
      {
        List localList = ScheduleContext.getJdbcDaoManager().listByFieldValue(ScheduleTask.class, "groupId", new Integer(j));
        int k = 0;
        int l = localList.size();
        while (k < l)
        {
          deleteTask((ScheduleTask)localList.get(k));
          ++k;
        }
        ScheduleContext.getJdbcDaoManager().deleteById(ReportletEntry.class, j);
      }
      else if (str.equals("1"))
      {
        ScheduleContext.getJdbcDaoManager().deleteById(FileEntry.class, j);
      }
      else if (str.equals("3"))
      {
        ScheduleContext.getJdbcDaoManager().deleteById(URLEntry.class, j);
      }
    }
  }

  public static JSONArray getTasks(String paramString)
  {
    JSONArray localJSONArray = new JSONArray();
    long l = Long.parseLong(paramString);
    List localList = ScheduleContext.getJdbcDaoManager().listByFieldValue(ScheduleTask.class, "groupId", new Long(l));
    for (int i = 0; i < localList.size(); ++i)
    {
      ScheduleTask localScheduleTask = (ScheduleTask)localList.get(i);
      try
      {
        JSONObject localJSONObject = localScheduleTask.createJSONConfig();
        localJSONArray.put(localJSONObject);
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
    }
    return localJSONArray;
  }

  public static void addOrUpdateTask(String paramString)
  {
    JSONObject localJSONObject1;
    try
    {
      localJSONObject1 = new JSONObject(paramString);
      long l = localJSONObject1.getLong("groupId");
      String str1 = localJSONObject1.getString("name");
      String str2 = localJSONObject1.getString("description");
      Object localObject = null;
      JSONObject localJSONObject2 = localJSONObject1.getJSONObject("iTrigger");
      String str3 = localJSONObject2.getString("recurrenceType");
      if ("once".equals(str3))
        localObject = OnceITrigger.analyzeJSON(localJSONObject2);
      else if ("simple".equals(str3))
        localObject = SimpleITrigger.analyzeJSON(localJSONObject2);
      else if ("calendar".equals(str3))
        localObject = CalendarITrigger.analyzeJSON(localJSONObject2);
      JSONArray localJSONArray = localJSONObject1.getJSONArray("taskParameters");
      TransmitParameters localTransmitParameters = TransmitParameters.parseJSON(localJSONArray);
      JSONObject localJSONObject3 = localJSONObject1.getJSONObject("scheduledOutput");
      ScheduledOutput localScheduledOutput = ScheduledOutput.analyzeJSON(localJSONObject3);
      ScheduleTask localScheduleTask = null;
      ITrigger localITrigger = null;
      if (localJSONObject1.has("id"))
      {
        localScheduleTask = (ScheduleTask)ScheduleContext.getJdbcDaoManager().load(ScheduleTask.class, localJSONObject1.getLong("id"));
        localITrigger = localScheduleTask.getITrigger();
        localScheduleTask.setGroupId(l);
        localScheduleTask.setName(str1);
        localScheduleTask.setDescription(str2);
        localScheduleTask.setITrigger((ITrigger)localObject);
        localScheduleTask.setTaskParameters(localTransmitParameters);
        localScheduleTask.setScheduledOutput(localScheduledOutput);
      }
      else
      {
        localScheduleTask = new ScheduleTask(l, str1, str2, (ITrigger)localObject, localTransmitParameters, localScheduledOutput);
      }
      ScheduleContext.getJdbcDaoManager().saveOrUpdate(localScheduleTask);
      Scheduler localScheduler = ScheduleContext.getScheduler();
      ReportletEntry localReportletEntry = (ReportletEntry)ScheduleContext.getJdbcDaoManager().load(ReportletEntry.class, l);
      JobDetail localJobDetail = new JobDetail(localScheduleTask.getName(), localReportletEntry.getDisplayName(), localReportletEntry.getReportJobClass());
      localJobDetail.setDescription(localScheduleTask.getDescription());
      localJobDetail.getJobDataMap().put("__task__", localScheduleTask.getId());
      localJobDetail.getJobDataMap().put("__reportlet__", localReportletEntry.getId());
      Trigger localTrigger = ((ITrigger)localObject).createTrigger();
      localTrigger.setJobName(localJobDetail.getName());
      localTrigger.setJobGroup(localJobDetail.getGroup());
      try
      {
        Trigger[] arrayOfTrigger = localScheduler.getTriggersOfJob(localJobDetail.getName(), localJobDetail.getGroup());
        if (arrayOfTrigger.length > 0)
          localScheduler.rescheduleJob(localITrigger.createTrigger().getName(), localITrigger.createTrigger().getGroup(), localTrigger);
        else
          localScheduler.scheduleJob(localJobDetail, localTrigger);
      }
      catch (SchedulerException localSchedulerException)
      {
        localSchedulerException.printStackTrace();
      }
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
  }

  public static void delTasks(String paramString)
  {
    String[] arrayOfString = paramString.split(",");
    for (int i = 0; i < arrayOfString.length; ++i)
    {
      long l = Long.parseLong(arrayOfString[i]);
      Object localObject = ScheduleContext.getJdbcDaoManager().load(ScheduleTask.class, l);
      deleteTask((ScheduleTask)localObject);
    }
  }

  private static void deleteTask(ScheduleTask paramScheduleTask)
  {
    Scheduler localScheduler = ScheduleContext.getScheduler();
    long l = paramScheduleTask.getGroupId();
    ReportletEntry localReportletEntry = (ReportletEntry)ScheduleContext.getJdbcDaoManager().load(ReportletEntry.class, l);
    try
    {
      localScheduler.deleteJob(paramScheduleTask.getName(), localReportletEntry.getDisplayName());
    }
    catch (SchedulerException localSchedulerException)
    {
      FRContext.getLogger().log(Level.WARNING, localSchedulerException.getMessage(), localSchedulerException);
    }
    ScheduleContext.getJdbcDaoManager().delete(paramScheduleTask);
  }

  public static JSONArray genParameters(String paramString)
  {
    JSONArray localJSONArray = new JSONArray();
    long l = Long.parseLong(paramString);
    ReportletEntry localReportletEntry = (ReportletEntry)ScheduleContext.getJdbcDaoManager().load(ReportletEntry.class, l);
    String str = localReportletEntry.getReportletPath();
    TemplateWorkBook localTemplateWorkBook = null;
    try
    {
      localTemplateWorkBook = FRContext.getCurrentEnv().readTemplate(str);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    Parameter[] arrayOfParameter = localTemplateWorkBook.getParameters();
    if (arrayOfParameter.length > 0)
    {
      TransmitParameters localTransmitParameters = new TransmitParameters(arrayOfParameter);
      try
      {
        localJSONArray = localTransmitParameters.createJSON();
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
    }
    return localJSONArray;
  }

  public static JSONArray getParameters(String paramString)
  {
    JSONArray localJSONArray = new JSONArray();
    TemplateWorkBook localTemplateWorkBook = null;
    try
    {
      localTemplateWorkBook = FRContext.getCurrentEnv().readTemplate(paramString);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    Parameter[] arrayOfParameter = localTemplateWorkBook.getParameters();
    if (arrayOfParameter.length > 0)
    {
      TransmitParameters localTransmitParameters = new TransmitParameters(arrayOfParameter);
      try
      {
        localJSONArray = localTransmitParameters.createJSON();
      }
      catch (JSONException localJSONException)
      {
        localJSONException.printStackTrace();
      }
    }
    return localJSONArray;
  }

  public static int stateJob(long paramLong)
  {
    int i = -1;
    Scheduler localScheduler = ScheduleContext.getScheduler();
    ScheduleTask localScheduleTask = (ScheduleTask)ScheduleContext.getJdbcDaoManager().load(ScheduleTask.class, paramLong);
    if ((localScheduleTask.getITrigger().getStartTime() != null) && (localScheduleTask.getITrigger().getStartTime().getTime() > System.currentTimeMillis()))
      return -2;
    ReportletEntry localReportletEntry = (ReportletEntry)ScheduleContext.getJdbcDaoManager().load(ReportletEntry.class, localScheduleTask.getGroupId());
    try
    {
      Trigger[] arrayOfTrigger = localScheduler.getTriggersOfJob(localScheduleTask.getName(), localReportletEntry.getDisplayName());
      if (arrayOfTrigger.length > 0)
      {
        Trigger localTrigger = arrayOfTrigger[0];
        i = localScheduler.getTriggerState(localTrigger.getName(), localTrigger.getGroup());
        if (i == 0)
          localScheduler.pauseJob(localTrigger.getJobName(), localTrigger.getJobGroup());
        else
          localScheduler.resumeJob(localTrigger.getJobName(), localTrigger.getJobGroup());
        i = localScheduler.getTriggerState(localTrigger.getName(), localTrigger.getGroup());
      }
    }
    catch (SchedulerException localSchedulerException)
    {
      localSchedulerException.printStackTrace();
    }
    return i;
  }
}